<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="类型" prop="type">
        <el-tree-select
          v-model="queryParams.type"
          :data="categoryList"
          :render-after-expand="false"
          :props="{ label: 'name', value: 'id' }"
          style="width: 240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['article:index:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="articleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title">
        <template #default="scope">
          <!-- 小于12位默认展示 -->
          <div v-if="scope.row.title != null && scope.row.title.length <= 10">
            {{ scope.row.title }}
          </div>
          <!-- 大于12为只展示12位 -->
          <el-tooltip
            v-if="scope.row.title != null && scope.row.title.length > 10"
            popper-class="testtooltip"
            :content="scope.row.title"
            placement="top-start"
          >
            <div>{{ scope.row.title.slice(0, 10) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="typeName" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="scan" />
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['article:index:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['article:index:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="articleRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <!-- <el-tree-select
            v-model="form.type"
            :data="categoryList"
            :render-after-expand="false"
            @change="handleTypeChange"
            :props="{ label: 'name', value: 'id' }"
            style="width: 240px"
          /> -->
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>
        <el-form-item label="简介" prop="memo">
          <el-input v-model="form.memo" type="textarea" :rows="10" />
        </el-form-item>
        <el-form-item label="详情" prop="intro">
          <editor v-model="form.intro" :min-height="500" />
        </el-form-item>

        <el-form-item label="跳转链接" prop="link">
          <el-input v-model="form.link" placeholder="请输入跳转链接" />
        </el-form-item>
        <el-form-item label="浏览量" prop="scan">
          <el-input-number v-model="form.scan" :min="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Article">
// import {
//   listArticle,
//   getArticle,
//   delArticle,
//   addArticle,
//   updateArticle,
// } from "@/api/main/article";
import { listCategory } from "@/api/main/category";
const { proxy } = getCurrentInstance();

const articleList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const categoryList = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    intro: null,
    image: null,
    scan: null,
    memo: null,
    status: null,
    createUser: null,
    createName: null,
    updateUser: null,
    updateName: null,
  },
  rules: {
    title: [{ required: true, message: "标题不能为空", trigger: "change" }],
    type: [{ required: true, message: "类型不能为空", trigger: "change" }],
    image: [{ required: true, message: "图片不能为空", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询文章列表 */
function getList() {
  loading.value = true;
  listArticle(queryParams.value).then((response) => {
    articleList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// function getCategoryList() {
//   listCategory({ pid: 0, type: 0 }).then((res) => {
//     categoryList.value = res.data;
//   });
// }
function handleTypeChange(data) {
  const type = categoryList.value.filter((temp) => temp.id === data);
  if (type.length > 0) {
    form.value.typeName = type[0].name;
  } else {
    form.value.typeName = null;
  }
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    title: null,
    intro: null,
    image: null,
    scan: null,
    memo: null,
    status: null,
    delFlag: null,
    createUser: null,
    createName: null,
    createTime: null,
    updateUser: null,
    updateName: null,
    updateTime: null,
  };
  proxy.resetForm("articleRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加文章";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getArticle(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改文章";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["articleRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateArticle(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArticle(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除文章编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delArticle(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "article/article/export",
    {
      ...queryParams.value,
    },
    `article_${new Date().getTime()}.xlsx`
  );
}

getList();
// getCategoryList();
</script>
